package org.hot100_2;

import java.util.HashMap;
import java.util.Stack;

public class leetcode20 {

    public boolean isValid(String s) {
        int n = s.length();

        if (n%2!=0){
            return false;
        }

        HashMap<Character,Character> map = new HashMap<>();
        map.put('(',')');
        map.put('{','}');
        map.put('[',']');

        //向stack中存放右括号
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);

            if (map.containsKey(ch)){
                stack.push(map.get(ch));
            }else {
                if (stack.isEmpty() || stack.peek()!=ch){
                    return false;
                }

                stack.pop();
            }
        }

        return stack.isEmpty();
    }
}
